<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            margin:0;
            padding:0;
        }

        .dialog{
            width:300px;
            height: 300px;
            position: absolute;
            top:200px;
            left:200px;
            border:1px solid gray;
        }
    </style>
</head>
<body>
    <!-- <div class="dialog">
        <h1>是否删除吗？</h1>
        <div>
            <button>取消</button>
            <button>确定</button>
        </div>
    </div> -->
    <script>
        class Dialog{
            constructor(options){
                this.options = options;
                this.init();//初始化
            }

            init(){
                //创建dom
                this.createElement();
                //添加事件
                this.addEvent();
            }

            createElement(){
                this.parentNode = document.createElement('div');
                this.parentNode.classList.add('dialog');
                this.parentNode.innerHTML = `
                    <h1>${this.options.title}</h1>
                    <div>
                        ${this.options.btns.map(item => `<button>${item}</button>`).join('')} 
                    </div>
                `;
                document.body.appendChild(this.parentNode);
            }

            addEvent(){
                let buttons = [...document.querySelectorAll('button')];
                console.log(buttons);
                buttons.forEach((item,index) => {
                    //bind call apply
                    item.onclick = this.options['cb'+index].bind(this)
                })
            }
        }

        let dialog = new Dialog({
            title:'是否要删除？',
            btns:['取消','确定'],
            cb0(){
                console.log("取消");
                document.body.removeChild(this.parentNode);
            },
            cb1(){
                console.log("确定")
            }
        })
    </script>
</body>
</html>